<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8" />
    <title>微信商户</title>
    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
	<link rel="stylesheet" type="text/css" href="/static/basic/plugins/easyui/themes/bootstrap/easyui.css">
	<link href="https://cdn.staticfile.org/font-awesome/5.11.2/css/all.min.css" rel="stylesheet">
	<link rel="stylesheet" type="text/css" href="/static/basic/plugins/webuploader/webuploader.css?v=12171">
	<link rel="stylesheet" type="text/css" href="/view/gas/station/style.css?v=1">
	<link rel="stylesheet" type="text/css" href="/static/m.css" >
	<link rel="stylesheet" type="text/css" href="/static/0316/iconfont/iconfont.css?v=1" >
	
</head>
<body>
<div id="window-mmask" class="window-mmask" ><div class="window-mmask-msg" >页面加载中,请稍候...</div></div>
<div class="easyui-layout" data-options="fit:true"  >
	<div data-options="region:'center'" >
		<table id="gridList" ></table>
	</div>
</div>
<div id="searchbar">
<div class="maction">
	<a href="javascript:void(0)" id="addButton" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-plus mbutton'">新增</a>
	<a href="javascript:void(0)" id="editButton" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-pencil-alt mbutton'">编辑</a>
	<a href="javascript:void(0)" id="enabledButton" class="easyui-linkbutton" data-options="plain:true,iconCls:'iconfont icon-qiyong mbutton'">启用</a>
	<a href="javascript:void(0)" id="disableButton" class="easyui-linkbutton" data-options="plain:true,iconCls:'iconfont icon-jinyong mbutton color-c5'">禁用</a>
		<!-- <a href="javascript:void(0)" id="removeButton" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-trash-alt mbutton color-c5'">删除</a> -->
</div>
<form id="searchForm" >
	<!-- 查询列<input id="search-字段名" name="字段名" class="easyui-textbox" >
	查询列<input id="search-字段名" name="字段名" class="easyui-textbox" > -->
	<a id="searchButton" href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true,iconCls:'fas fa-search mbutton'" >查询</a>
</form>
</div>
<script src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js"></script>
<script src="/static/basic/plugins/easyui/jquery.easyui.min.js"></script>
<script src="/static/init.js"></script>
<script src="/static/basic/plugins/easyui/locale/easyui-lang-zh_CN.js"></script>	
<script src="/static/searchEdit.js"></script>
<script type="text/javascript" src="/static/basic/plugins/webuploader/webuploader.html5only.min.js"></script>
<!-- <script src="https://cdn.staticfile.org/moment.js/2.24.0/moment.min.js"></script> -->
<script>
var options={
	page_id:216,
	grid_id:"gridList",
	sqlname:"grid_216_wx_merchant",
	showCheckbox:false,
	rownumbers:false,
	column_formatter:{
		is_disabled:function(value,row,index){
			if(value==0){
				return "<span class='iconfont icon-qiyong mbutton'></span>";
			}else{
				return "<span class='iconfont icon-jinyong mbutton color-c5'></span>";
			}
		}
	},
};
var editRow=null;
//document ready
$(function(){
	grid1=new uniontrip.datagrid.searchEdit(options);
	//tab
	$('#addButton').bind('click', function(){
		saveUrl="/authenticated/wxmerchant/add";
		editRow=null;
		EditDialog.dialog('setTitle','新增微信商户').dialog('open');
	});//新增行绑定事件
	$('#editButton').bind('click',function(){
		saveUrl="/authenticated/wxmerchant/update";
		gridSelectRow(grid1.grid,function(row){
			editRow=row;
			EditDialog.dialog('setTitle','修改微信商户信息').dialog('open');
		});
	});//编辑按钮绑定事件
	$('#removeButton').bind('click',function(){gridConfirm(grid1,"是否确认删除？",deleteSubmit);});//删除行绑定事件
	//禁用
	$('#disableButton').bind('click',function(){
		gridSelectRow(grid1.grid,function(row){
			editRow=row;
			if(row.is_disabled==1){
				info("已经是禁用状态！");
				return;
			}
			gridConfirm(grid1.grid,"是否确认禁用商户号",function(row){
				$.post("/authenticated/wxmerchant/disable",{"mch_id":row.mch_id},function(result){
					if(result.success){
						info("禁用成功！");
						grid1.load();
					}else{
						$.messager.alert("提示", "禁用失败！<br>"+result.message);
					}
				});
			});
		});
	});
	//启用
	$('#enabledButton').bind('click',function(){
		gridSelectRow(grid1.grid,function(row){
			editRow=row;
			if(row.is_disabled==0){
				info("已经是启用状态！");
				return;
			}
			gridConfirm(grid1.grid,"是否确认启用商户号",function(row){
				$.post("/authenticated/wxmerchant/enable",{"mch_id":row.mch_id},function(result){
					if(result.success){
						info("启用成功！");
						grid1.load();
					}else{
						$.messager.alert("提示", "启用失败！<br>"+result.message);
					}
				});
			});
		});
	});
	$('#searchButton').bind('click',grid1.load);//查询操作绑定事件
	//row
	//$('#addButton').bind('click', addRow);//新增行绑定事件
	//$('#editButton').bind('click',grid1.editRow);//编辑按钮绑定事件
	$('#removeButton').bind('click',grid1.remove);//删除行绑定事件
	$('#saveButton').bind('click', grid1.accept);//保存datagrid绑定事件
	$('#cancelEditButton').bind('click',grid1.cancelEditing);//取消行编辑绑定事件
	$('#endEditingButton').bind('click', grid1.endEditing);//结束行编辑绑定事件
	$('#rejectButton').bind('click', grid1.rejectChanges);//撤销编辑绑定事件		
  $('#showChangesButton').bind('click',grid1.showChanges);//信息变更提示

  //初始化搜索框
  initsearch();
  EditDialog=$("<div/>").appendTo("body").dialog({
		title:"新增",
		cls:"m-dialog",
		width:'400px',
		height:windowHeight*0.8,
		//href:'/grid/form/insert/view?grid_id=216',
		href:'216-edit.html',
		modal:true,
		closed:true,
		maximized:false,
		onClose:function(){
			console.log('onClose');
			
		},
		onBeforeClose:function(){
			$("#form-1").form('clear');
		},
		 buttons: [{
			text:'保存',
			iconCls:'fas fa-save mbutton',
			handler:save
		},{
			text:'取消',
			iconCls:'fas fa-times mbutton',
			handler:function(){
				EditDialog.dialog('close')
			}
		}],
		onOpened:function(){
			console.log('onOpened');
			if(editRow){
				loadForm();
				//1.显示地址和标注
				$('#mch_id').textbox('readonly',true);
	  			
			}else{
				$('#mch_id').textbox('readonly',false);
			}
			
		},
		onOpen:function(){
			console.log('onOpen');
			if($(this).data("panel").isLoaded){
				$(this).panel('options').onOpened();
			}
		},
		onLoad:function(){
			console.log('onLoad');
			$(this).panel('options').onOpened();
			initWebUploader();
		}
	});
});
function loadForm(){
	if(editRow){
		//console.log(editRow);
		$("#form-1").form('load',editRow);
	}
}
function initWebUploader(pickfiles){
	//初始化Web Uploader
	var uploader = WebUploader.create({

		// 选完文件后，是否自动上传。
	    auto: false,
	    // 文件接收服务端。
	    server: '/authenticated/upload',

	    // 选择文件的按钮。可选。
	    // 内部根据当前运行是创建，可能是input元素，也可能是flash.
	    pick: {
               id: "#picker"
        },
        formData: {
            file_id:16
        },
	    // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
	    resize: false
	});
	// 当有文件被添加进队列的时候
	uploader.on( 'fileQueued', function( file ) {
	    $("#thelist").append( '<div id="' + file.id + '" class="item">' +
	        '<h4 class="info">' + file.name + '</h4>' +
	        '<p class="state">等待上传...</p>' +
	    '</div>' );
	});

	// 文件上传过程中创建进度条实时显示。
	uploader.on( 'uploadProgress', function( file, percentage ) {
	    var $li = $( '#'+file.id ),
	        $percent = $li.find('.progress .progress-bar');

	    // 避免重复创建
	    if ( !$percent.length ) {
	        $percent = $('<div class="progress progress-striped active">' +
	          '<div class="progress-bar" role="progressbar" style="width: 0%">' +
	          '</div>' +
	        '</div>').appendTo( $li ).find('.progress-bar');
	    }

	    $li.find('p.state').text('上传中');

	    $percent.css( 'width', percentage * 100 + '%' );
	});

	uploader.on( 'uploadSuccess', function( file,response ) {
		$("#cert_id").val(response.data.item_id);
	    $( '#'+file.id ).find('p.state').text('已上传');
	});

	uploader.on( 'uploadError', function( file ) {
	    $( '#'+file.id ).find('p.state').text('上传出错');
	});

	uploader.on( 'uploadComplete', function( file ) {
	    $( '#'+file.id ).find('.progress').fadeOut();
	});
	
	uploader.on('uploadBeforeSend', function(object,data,headers) {
        let access_token=localStorage.getItem("access_token");
        headers.Authorization=access_token;
    });
	$('#deleteBtn').bind('click',function(){
		uploader.reset();
		$("#cert_id").val("");
		$("#thelist").html("");
    });
	
	$('#ctlBtn').bind('click',function(){
		uploader.upload();
    });
}
var saveUrl="";
function save(){
	
	if($("#form-1").form('enableValidation').form('validate')){
		$.post(saveUrl,$("#form-1").serializeObject(),function(result){
			if(result.success){
				info( "保存成功！");
				EditDialog.dialog('close');
				grid1.load();
			}else{
				$.messager.alert("提示", "保存失败！<br>"+result.message);
			}
		});
	}
}
/**
* 新增一行
*/
function addRow(){
//ROW
	var newRowData={id:guid()};//初始化行数据,creator_id:sessionuser.id,creator_name:sessionuser.nickname,create_time:moment().format('YYYY-MM-DD')
	grid1.grid.datagrid('append',newRowData);	
	//tab
	parent.addTab("新增","${relative_path}form.html");
}
function onUpdate(){
	var selectRows=grid1.grid.datagrid('getSelections');
	if(selectRows.length>0){
		parent.addTab("编辑["+selectRows[0].id+"]","${relative_path}form.html?id="+selectRows[0].id);
	}else{
		$.messager.alert("提示", "请选择一条数据！");
	}
}
/**
* 删除提交
* @param selectRow
*/
function deleteSubmit(selectRow){
	$.post("/authenticated/ajax/update/grid_${table_name}-delete",JSON.stringify({ids:[selectRow.id]}),function(result){
		if(result.status==200){
			$.messager.alert("提示", "删除成功！");
			grid1.load();
		}
	});
}

function onDeleteRow(){
	gridConfirm(grid1.grid,"是否确认删除",deleteRow);
}
function deleteRow(row){
	$.post("/authenticated/xx/delete",JSON.stringify({"id":row.id}),function(result){
		if(result.status==200){
			$.messager.alert("提示", "删除成功！");
			grid1.load();
		}else{
			$.messager.alert("提示", "删除失败！<br>"+result.message);
		}
	});
}


/**
* 初始化搜索条件
*/
function initsearch(){
	/*$('#search-product_class_id').combotree({
		panelHeight:'auto',
		icons: [{
			iconCls:'icon-clear',
			handler:function(){
				$('#search-product_class_id').combotree('clear');
			}
		}],
	    data:grid1.combotreeDatas.product_class_id
	});
	//初始化combobox例子
	$('#search-status').combobox({
		panelHeight:'auto',
		editable:false,
		valueField:'code_value',
		textField:'code_name',
		icons: [{
			iconCls:'icon-clear',
			handler:function(e){
				$(e.data.target).combobox('clear');
			}
		}],
	    data:grid1.comboboxDatas.status
	});*/
}
</script>
</body>
</html>